package sketchpad.gui.component.event;

import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import sketchpad.business.function.LabelService;
import sketchpad.business.function.impl.LabelServiceImpl;
import sketchpad.constant.CHNName;
import sketchpad.constant.Geometry;
import sketchpad.constant.Name;
import sketchpad.gui.control.DrawControl;
import sketchpad.business.function.dto.UpLoadImgReturnDTO;
import sketchpad.gui.component.Dialog;
import sketchpad.gui.component.Spinner;

//本来将组件的监听事件抽离出来减少代码重复
public class EventCenter {

    private EventCenter(){}

    public static void labelEventHandler(Stage stage, DrawControl control){
        //弹出对话框获得输入
        if (stage!=null && stage.isShowing())stage.close();
        StackPane stackPane = new StackPane();
        Scene scene = new Scene(stackPane,400,200);
        stage.setScene(scene);
        Dialog dialog = new Dialog(control,stage,stackPane);
        for (Integer id : control.getSelectedItemList()){
            if (control.getDrawItemMap().get(id).getGeometryClass()!= Geometry.RANDOM){
                dialog.disableChooseBox(true);
                break;
            }
        }
        dialog.show("请选择正确的标签信息，按确定键以确认:)");
        if (stage.isShowing())return;
        stage.showAndWait();
    }

    public static void recognizeEventHandler(Stage stage, DrawControl control){
        if (stage!=null && stage.isShowing())stage.close();
        StackPane Pane = new StackPane();
        Spinner spinner = new Spinner(Pane);
        Pane.setAlignment(Pos.CENTER);
        Pane.getChildren().add(new Label("识别中，请等待"));
        Scene scene = new Scene(Pane,400,200);
        stage.setScene(scene);
        stage.show();

        LabelService labelService = new LabelServiceImpl(control);
        UpLoadImgReturnDTO dto = labelService.recognize();
        if (dto==null)return;
        String name = "";
        switch (dto.getName()){
            case Name.RECTANGLE:name= CHNName.RECTANGLE;break;
            case Name.TRIANGLE:name=CHNName.TRIANGLE;break;
            case Name.CIRCLE:name=CHNName.CIRCLE;break;
            default:name="未知图形";
        }
        StackPane stackPane = new StackPane();
        scene = new Scene(stackPane,400,200);
        stage.setScene(scene);
        Dialog dialog = new Dialog(control,stage,stackPane);
        dialog.setDefaultItem(dto.getName());
        dialog.show("机器识别图形为："+ name +"\n置信度为          ："+String.format("%.2f",dto.getScore()*100)
                +"%\n机器识别有限，如果不正确请更改：");
    }
}
